library(ggplot2)
library(readxl)
library(readr)
library(tidyverse)
library(dplyr)
library(sf)
library(plotly)
library(geojsonio)
library(knitr)
library(tidyr)
# Load the dataset
df_descriptive=read_csv("data_final.csv")
data_final <- read_csv("data_final.csv")
# Table showing Top 10 NTAs with the Highest Total Shooting Incidents
top_10_nta_overall <- df_descriptive %>%
group_by(BORO, NTA) %>%
summarise(total_incidents = n(),
total_population = first(Total_population_nta),
percent_poverty = first(Percent_poverty),
percent_education = first(Percent_education)) %>%
arrange(desc(total_incidents)) %>%
ungroup() %>%
slice(1:10) %>%
select(BORO, NTA, total_incidents, total_population, percent_poverty, percent_education)
# Display the table in the desired format
kable(top_10_nta_overall, digits = 2, caption = "Top 10 NTAs with the Highest Total Shooting Incidents (2017-2023)")
| BORO | NTA | total_incidents | total_population | percent_poverty | percent_education |
|---|---|---|---|---|---|
| BROOKLYN | Brownsville | 350 | 60470 | 37.2 | 76.9 |
| BROOKLYN | Bedford-Stuyvesant (East) | 323 | 84653 | 22.2 | 87.8 |
| MANHATTAN | Harlem (North) | 253 | 83327 | 25.1 | 82.9 |
| BROOKLYN | Crown Heights (North) | 235 | 85275 | 20.5 | 84.1 |
| MANHATTAN | East Harlem (North) | 229 | 64655 | 32.3 | 75.9 |
| BRONX | Mott Haven-Port Morris | 207 | 57718 | 43.7 | 62.0 |
| BROOKLYN | Bedford-Stuyvesant (West) | 207 | 89189 | 26.0 | 84.8 |
| BROOKLYN | East New York-New Lots | 205 | 53004 | 25.3 | 82.6 |
| BRONX | Concourse-Concourse Village | 169 | 69387 | 31.9 | 70.5 |
| BRONX | Mount Eden-Claremont (West) | 166 | 49651 | 31.8 | 67.9 |
# Get the Top 10 NTAs with the Highest Incident Rate by Year
top_10_incident_rate <- df_descriptive %>%
distinct(incident_rate_by_year_nta, .keep_all = TRUE) %>%
filter(NTAType == 'Residential') %>%
arrange(desc(incident_rate_by_year_nta)) %>%
slice(1:10)
# Pull information for the Top 10 NTAs
top_10_nta_incident_rate <- top_10_incident_rate %>%
select(NTA, BORO,Year,incident_rate = incident_rate_by_year_nta,
total_population = Total_population_nta,
percent_poverty = Percent_poverty,
percent_education = Percent_education)
# Display the table in the desired format
kable(top_10_nta_incident_rate, digits = 5, caption = "Top 10 NTAs with the Highest Incident Rate in Each Borough")
| NTA | BORO | Year | incident_rate | total_population | percent_poverty | percent_education |
|---|---|---|---|---|---|---|
| Claremont Village-Claremont (East) | BRONX | 2021 | 0.18735 | 24553 | 43.2 | 61.7 |
| Brownsville | BROOKLYN | 2020 | 0.15380 | 60470 | 37.2 | 76.9 |
| Tremont | BRONX | 2020 | 0.11820 | 32150 | 36.3 | 71.4 |
| Inwood | MANHATTAN | 2021 | 0.11571 | 36299 | 15.3 | 77.0 |
| East New York-New Lots | BROOKLYN | 2020 | 0.10754 | 53004 | 25.3 | 82.6 |
| Hunts Point | BRONX | 2021 | 0.10574 | 15131 | 34.3 | 72.2 |
| Mount Eden-Claremont (West) | BRONX | 2021 | 0.10070 | 49651 | 31.8 | 67.9 |
| Brownsville | BROOKLYN | 2022 | 0.09922 | 60470 | 37.2 | 76.9 |
| Brownsville | BROOKLYN | 2021 | 0.09757 | 60470 | 37.2 | 76.9 |
| Tremont | BRONX | 2023 | 0.09642 | 32150 | 36.3 | 71.4 |
# Table showing Top 10 total incident NTA in Each Borough
incident_by_nta_borough <- df_descriptive %>%
drop_na() %>%
group_by(BORO, NTA) %>%
summarise(total_incidents = n()) %>%
arrange(BORO, desc(total_incidents)) %>%
group_by(BORO) %>%
slice_max(n = 10, order_by = total_incidents) %>%
select(-total_incidents) %>%
group_by(BORO) %>%
mutate(row_num = row_number()) %>%
pivot_wider(names_from = BORO, values_from = NTA) %>%
unnest(cols = c(BRONX, BROOKLYN, MANHATTAN, QUEENS, `STATEN ISLAND`)) %>%
select(row_num, everything())%>%
slice(1:10)
# Display the table in the desired format
kable(incident_by_nta_borough, caption = "Top 10 NTAs with Total Shooting Incidents in Each Borough (2017-2023)")
| row_num | BRONX | BROOKLYN | MANHATTAN | QUEENS | STATEN ISLAND |
|---|---|---|---|---|---|
| 1 | Mott Haven-Port Morris | Brownsville | East Harlem (North) | Far Rockaway-Bayswater | St. George-New Brighton |
| 2 | Mount Hope | Bedford-Stuyvesant (East) | Harlem (North) | Baisley Park | Tompkinsville-Stapleton-Clifton-Fox Hills |
| 3 | Tremont | Crown Heights (North) | East Harlem (South) | Richmond Hill | West New Brighton-Silver Lake-Grymes Hill |
| 4 | Mount Eden-Claremont (West) | East New York-New Lots | Harlem (South) | Jamaica | Mariner’s Harbor-Arlington-Graniteville |
| 5 | Concourse-Concourse Village | East Flatbush-Remsen Village | Washington Heights (South) | Rockaway Beach-Arverne-Edgemere | Annadale-Huguenot-Prince’s Bay-Woodrow |
| 6 | Williamsbridge-Olinville | East New York (North) | Chelsea-Hudson Yards | St. Albans | Port Richmond |
| 7 | Melrose | Bedford-Stuyvesant (West) | Hamilton Heights-Sugar Hill | South Ozone Park | Rosebank-Shore Acres-Park Hill |
| 8 | Belmont | Canarsie | Inwood | Elmhurst | Grasmere-Arrochar-South Beach-Dongan Hills |
| 9 | Longwood | Coney Island-Sea Gate | Chinatown-Two Bridges | South Jamaica | Great Kills-Eltingville |
| 10 | Fordham Heights | East New York-City Line | Washington Heights (North) | Astoria (East)-Woodside (North) | New Dorp-Midland Beach |
data_final <- read_csv("data_final.csv")
boro_map_data <- boro_map_data %>%
mutate(
hover_text = paste("Borough:", boro_name, "<br>Total Incidents:", Number_of_Incidents)
)
# Create the interactive plot with click functionality
plot <- plot_ly(
data = boro_map_data,
type = "scattermapbox",
split = ~boro_name, # Separate polygons by boroughs
color = ~Number_of_Incidents, # Color based on the number of incidents
colors = "viridis", # Use a color scale
text = ~hover_text, # Display hover text
hoverinfo = "text",
marker = list(size = 8, opacity = 0.7)
) %>%
layout(
title = "Total Number of Incidents Across NYC BOROs (2017-2023)",
mapbox = list(
style = "carto-positron", # Base map style
center = list(lon = -74.00, lat = 40.7128), # Center map on NYC
zoom = 9
)
)
# Add click functionality to display the borough name and number of incidents
plot <- plot %>%
event_register("plotly_click") %>%
htmlwidgets::onRender("
function(el, x) {
el.on('plotly_click', function(d) {
var point = d.points[0];
var text = point.text;
alert('You clicked on: ' + text);
});
}
")
# Display the interactive plot
plot
<<<<<<< HEAD
=======